package com.jy.wms.service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.PinyinUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.InstockDetailed;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsInstockDetailedReportMapper;
import com.jy.wms.pojo.InstockDetailedReport;
import com.jy.wms.pojo.WmsOwnerOrdertype;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by syh on 2018/4/11.
 */
@Service
public class WmsInstockDetailedReportService extends BaseService<WmsInstockDetailedReportMapper,InstockDetailedReport> {
    /**
     * 入库明细报表 分页查询
     * @param entity
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "入库明细报表",btnName = "分页查询入库明细报表")
    public Map<String, Object> baseSelectPage(InstockDetailedReport entity) throws ServiceException {
        try{
            Page<InstockDetailedReport> pageList = new Page<InstockDetailedReport>();
            List<Map<String, Object>> list = new ArrayList<>();
            Map map1 = new HashMap();
            if(entity.getCjsj_start()!=null && !"".equals(entity.getCjsj_start())){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar c = Calendar.getInstance();
                c.setTime(new Date(Long.valueOf(entity.getCjsj_start())));
                c.set(Calendar.HOUR_OF_DAY, 00);
                c.set(Calendar.MINUTE, 00);
                c.set(Calendar.SECOND, 00);
                entity.setCjsj_start(sdf.format(c.getTime()));

                c.setTime(new Date(Long.valueOf(entity.getCjsj_end())));
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                entity.setCjsj_end(sdf.format(c.getTime()));
            //分页查询
             PageHelper.startPage(entity.getPage(),entity.getRows());
             pageList=(Page<InstockDetailedReport>)dao.baseSelectPage(entity);
            for(InstockDetailedReport instockDetailedReport:pageList){
                getGongtong(entity, list, instockDetailedReport);
                }
            }
            map1.put("list", list);
            map1.put("totals", pageList.getTotal());
            return map1;
        }catch(Exception e){
            throw  new ServiceException("查询入库明细报表失败",e);
        }
    }

    /**
     *
     * 分页查询和导出取出相同的地方
     * @param entity
     * @param list
     * @param instockDetailedReport
     */
    public void getGongtong(InstockDetailedReport entity, List<Map<String, Object>> list, InstockDetailedReport instockDetailedReport)throws Exception {
        Map map;
        map = new HashMap();
        //入库汇总
        String rkhz=dao.getRkhz(instockDetailedReport,entity.getCjsj_start(),entity.getCjsj_end(),entity.getHzid(),entity.getCkid());
        //期初库存上架数量
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
        String mon =d.format(DateFormat.getDateInstance().parse(entity.getCjsj_start()));
        String mon1 = d.format(DateFormat.getDateInstance().parse(entity.getCjsj_end()));
        Integer qckc= dao.getQckc(instockDetailedReport,mon);
        Integer qmkc= dao.getQmkc(instockDetailedReport,mon1);
        //用产品编号 查订单类型
        List<Map<String, Object>> list3=dao.selectDdlx(instockDetailedReport.getCpbh(),entity.getHzid(),entity.getCkid(),entity.getCjsj_start(),entity.getCjsj_end());
        for(Map map3:list3){
            if(map3!=null){
            //订单类型的拼音
            String ddlx= PinyinUtils.ToPinyin(map3.get("ddlx").toString());
            map.put(ddlx,map3.get("shesl").toString());
            }
        }
        map.put("cpid",instockDetailedReport.getCpid());
        map.put("chanpinbianhao",instockDetailedReport.getCpbh());
        map.put("chanpinfenlei",instockDetailedReport.getCpfl());
        map.put("chanpinmingcheng",instockDetailedReport.getCpmc());
        map.put("guige",instockDetailedReport.getGg());
        map.put("danwei",instockDetailedReport.getDw());
        map.put("qichukucun",qckc);
        map.put("rukuhuizong",rkhz);
        map.put("qimokucun",qmkc);
        list.add(map);
    }


    /**
     * 表头创建
     * @param hzid
     * @param ckid
     * @return
     */
    public List<Map<String, Object>> listHeader(String hzid, String ckid) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map map = null;
        String minWidth = "0px";
        //中文汉字产品编号
        String cpbh=InstockDetailed.cpbh.getType();
        //中文首字母
        String cpbh2=PinyinUtils.ToPinyin(cpbh);
        map = header(map, cpbh, cpbh2, "true");
        mapList.add(map);
        //产品分类
        String cpfl=InstockDetailed.cpfl.getType();
        //中文首字母
        String cpfl2=PinyinUtils.ToPinyin(cpfl);
        map = header(map, cpfl, cpfl2, "false");
        mapList.add(map);
        //产品名称
        String cpmc=InstockDetailed.cpmc.getType();
        //中文首字母
        String cpmc2=PinyinUtils.ToPinyin(cpmc);
        map = header(map, cpmc, cpmc2, "true");
        mapList.add(map);
        //规格
        String gg=InstockDetailed.gg.getType();
        String gg2=PinyinUtils.ToPinyin(gg);
        map = header(map, gg, gg2, "false");
        mapList.add(map);
        //单位描述(主单位的)
        String dw=InstockDetailed.dw.getType();
        String dw2=PinyinUtils.ToPinyin(dw);
        map = header(map, dw, dw2, "false");
        mapList.add(map);
        //期初库存
        String qckc=InstockDetailed.qckc.getType();
        String qckc2=PinyinUtils.ToPinyin(qckc);
        map = header(map, qckc, qckc2, "false");
        mapList.add(map);
        //入库类型
        List<WmsOwnerOrdertype> listType=dao.selectType(hzid,ckid);
        for(WmsOwnerOrdertype wmsOwnerOrdertype:listType){
            String ddlx=wmsOwnerOrdertype.getDdlx();
            String ddlx2=PinyinUtils.ToPinyin(ddlx);
            map = header(map, ddlx, ddlx2, "false");
            mapList.add(map);
        }
        //入库汇总
        String rkhz=InstockDetailed.rkhz.getType();
        String rkhz2=PinyinUtils.ToPinyin(rkhz);
        map = header(map, rkhz, rkhz2, "false");
        mapList.add(map);
        //期末库存
        String qmkc=InstockDetailed.qmkc.getType();
        String qmkc2=PinyinUtils.ToPinyin(qmkc);
        map = header(map, qmkc, qmkc2, "false");
        mapList.add(map);
        return mapList;
    }

    /**
     * 是否查询,拼键值
     * @param map
     * @param label
     * @param prop
     * @param search
     * @return
     */
    public Map<String, Object> header(Map<String, Object> map, String label, String prop, String search) {
        map = new HashMap();
        //汉字
        map.put("label", label);
        //拼音(键值)
        map.put("prop", prop);
        map.put("minWidth", "0px");
        map.put("cRemarks", "");
        //是否查询
        map.put("search", search);
        return map;
    }

    /**
     * 报表导出查询
     * @param entity
     * @return
     */
    public List<Map<String, Object>> getInstockExport(InstockDetailedReport entity)throws Exception{
        List<Map<String, Object>> list = new ArrayList<>();
        List<InstockDetailedReport> instockList=new ArrayList<>();
        if(entity.getCjsj_start()!=null && !"".equals(entity.getCjsj_start())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date(Long.valueOf(entity.getCjsj_start())));
            c.set(Calendar.HOUR_OF_DAY, 00);
            c.set(Calendar.MINUTE, 00);
            c.set(Calendar.SECOND, 00);
            entity.setCjsj_start(sdf.format(c.getTime()));

            c.setTime(new Date(Long.valueOf(entity.getCjsj_end())));
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            entity.setCjsj_end(sdf.format(c.getTime()));
            //导出查询
            instockList=dao.baseSelectPage(entity);
            for(InstockDetailedReport instockDetailedReport:instockList){
                getGongtong(entity, list, instockDetailedReport);
            }
        }
        return  list;
    }

    /**
     * (导出)表头创建
     * @param hzid
     * @param ckid
     * @return
     */
    public List<ExcelExportEntity> listHeader2(String hzid, String ckid) {
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        //中文汉字产品编号
        String cpbh=InstockDetailed.cpbh.getType();
        //中文首字母
        String cpbh2=PinyinUtils.ToPinyin(cpbh);
        entity.add(new ExcelExportEntity(cpbh, cpbh2));
        //产品分类
        String cpfl=InstockDetailed.cpfl.getType();
        //中文首字母
        String cpfl2=PinyinUtils.ToPinyin(cpfl);
        entity.add(new ExcelExportEntity(cpfl, cpfl2));
        //产品名称
        String cpmc=InstockDetailed.cpmc.getType();
        //中文首字母
        String cpmc2=PinyinUtils.ToPinyin(cpmc);
        entity.add(new ExcelExportEntity(cpmc, cpmc2));
        //规格
        String gg=InstockDetailed.gg.getType();
        String gg2=PinyinUtils.ToPinyin(gg);
        entity.add(new ExcelExportEntity(gg, gg2));
        //单位描述(主单位的)
        String dw=InstockDetailed.dw.getType();
        String dw2=PinyinUtils.ToPinyin(dw);
        entity.add(new ExcelExportEntity(dw, dw2));
        //期初库存
        String qckc=InstockDetailed.qckc.getType();
        String qckc2=PinyinUtils.ToPinyin(qckc);
        entity.add(new ExcelExportEntity(qckc, qckc2));
        //入库类型
        List<WmsOwnerOrdertype> listType=dao.selectType(hzid,ckid);
        for(WmsOwnerOrdertype wmsOwnerOrdertype:listType){
            //汉字
            String ddlx=wmsOwnerOrdertype.getDdlx();
            //拼音
            String ddlx2=PinyinUtils.ToPinyin(ddlx);
            entity.add(new ExcelExportEntity(ddlx, ddlx2));
        }
        //入库汇总
        String rkhz=InstockDetailed.rkhz.getType();
        String rkhz2=PinyinUtils.ToPinyin(rkhz);
        entity.add(new ExcelExportEntity(rkhz, rkhz2));
        //期末库存
        String qmkc=InstockDetailed.qmkc.getType();
        String qmkc2= PinyinUtils.ToPinyin(qmkc);
        entity.add(new ExcelExportEntity(qmkc, qmkc2));
        return entity;
    }
}